package com.cet.designapplication.deprecated.state;

import com.cet.designapplication.utils.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author NSOC
 */

@Component
public class DeprecatedWaitPay extends DeprecatedAbstractOrderState {

    @Autowired
    private RedisCommonProcessor redisCommonProcessor;

    @Autowired
    private DeprecatedWaitSend deprecateWaitSend;

    @Override
    protected DeprecatedOrder payOrder(String orderId, DeprecateOrderContext context) {

        DeprecatedOrder order = (DeprecatedOrder) redisCommonProcessor.get(orderId);
        if (!order.getState().equals(ORDER_WAIT_PAY)) {
            throw new UnsupportedOperationException("Order state expect to be ORDER_WAIT_PAY, now is " + order.getState());
        }

        // 支付逻辑

        order.setState(ORDER_WAIT_SEND);
        redisCommonProcessor.set(orderId, order);
        context.setCurrentState(deprecateWaitSend);
        notifyObservers(orderId, ORDER_WAIT_SEND);
        return order;
    }
}
